import * as layoutFetch from '../api/apiLayout'
import {findInArray, getHistory, getState, setState, setStateReducer} from "../util/uitls";
import {effects} from 'dva/saga'
import {GET_USER_DESIGN_IMAGES, POST_USER_DESIGN_IMAGES, PUT_USER_DESIGN_IMAGES_ID} from "../api/uploadCustomFabric";

const INIT_STATE = {
    imageList: {},
    editImageItem: {}
}
const Map = require('lodash.map')
const namespace = 'uploadCustomFabric'
const MODEL = {
    namespace,
    state: INIT_STATE,
    reducers: {
        setState: setStateReducer
    },
    effects: {
        * setSelectMenuKeys({payload: {pathname}}, {call, put}) {
            yield setState({
                selectMenuKeys: pathname
            })('layout')
        },
        * initImageList({}, {call, put}) {
            var data = yield call(GET_USER_DESIGN_IMAGES, {title: ""})
            yield setState({
                imageList: data
            }, true)(namespace)
        },
        * gotoEdit({payload}, {call, put}) {
            yield setState({
                editImageItem: null
            })(namespace)
            yield setState({
                editImageItem: payload
            })(namespace)
            getHistory().push(`/setting/custom-fabric/${payload.id}`)
        },
        * setItemRedux({payload}, {call, put}) {
            var data = yield  call(PUT_USER_DESIGN_IMAGES_ID, payload)
            yield setState({
                imageList: {
                    [data.id]: data
                }
            })(namespace)
        },
        * addItem({payload}, {call, put}) {
            var data = yield call(POST_USER_DESIGN_IMAGES, payload)
            yield setState({
                imageList: {
                    [data.id]: data
                }
            })(namespace)
        }
    },
    subscriptions: {
        setup({dispatch, history}) {
        },
    },
};

export default MODEL
